<?php
//文件名：lvmao_func.php
//用途：整合phpcms登陆和积分系统的基本函数

require_once dirname(__FILE__).'/include/common.inc.php';


//获得通用用户id
function GetId()
{
	global $db;
	global $_username;
	if($_username!="")
	{
		$sql="select id from user where username='".$_username."' limit 1";
		$re=$db->query($sql);
		$re=$db->fetch_array($re);
		
		return $re[id];
	}
	else
	{
		return "0";
	}
}

//同步通用积分和cms积分
function GoSync($uid)
{
	global $db;
	
	$sql="select sum(num) as numsum from score_log where uid=".$uid;
	$re=$db->query($sql);
	$re=$db->fetch_array($re);
	$nowscore=$re[numsum];
	
	$sql="update user set score=".$nowscore." where id=".$uid;
	$db->query($sql);
	
	$sql="select a.userid from zzinfo_member a inner join user b on b.id=".$uid." and a.username=b.username";
	$re=$db->query($sql);
	$re=$db->fetch_array($re);
	$userid=$re[userid];
	
	$sql="update ".DB_PRE."member set point=".$nowscore." where userid=".$userid;
	$db->query($sql);
	$sql="update ".DB_PRE."member_cache set point=".$nowscore." where userid=".$userid;
	$db->query($sql);
}


//php版积分操作函数
function AddScoreLog($uid,$num,$typeid,$objid,$oprid,$txt)
{
	global $db;
	
	
	//如果减分先判断够不够
	if($num<0)
	{
		$sql="select score from user where id=".$uid;
		$re=$db->query($sql);
		$re=$db->fetch_array($re);
		
		echo $re[score]+$num;
		if($re[score]+$num<0)
		{
			return false;
		}
	}

	
	//添加积分记录
	$sql="insert into score_log(uid,num,itime,typeid,oprid,objid,txt) values(".$uid.",".$num.",'".date("Y-m-d H:i:s")."',".$typeid.",".$oprid.",".$objid.",'".$txt."');";
	$db->query($sql);
	
	//更新积分缓存并同步到cms
	GoSync($uid);
	
	
	return true;
}

?>